package com.moefor.clover.init.config;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;

import antlr.debug.MessageAdapter;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.moefor.clover.controller",
				useDefaultFilters = false,
				includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {org.springframework.stereotype.Controller.class})}
)
public class DispatcherServletConfig extends WebMvcConfigurationSupport{
	
	@Autowired
	FastJsonHttpMessageConverter4 fastJsonMessageConverter;
	
	@Autowired
	MappingJackson2HttpMessageConverter jacksonMessageConverter;
	
	@Bean(name = "viewResolver")
	public InternalResourceViewResolver InternalResourceViewResolvern(){
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/view/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}
	
	@Bean(name = "multipartResolver")
	public CommonsMultipartResolver commonsMultipartResolver() throws IOException{
		CommonsMultipartResolver resolver = new CommonsMultipartResolver();
		resolver.setMaxUploadSize(1024 * 1024 * 5);
		resolver.setDefaultEncoding("UTF-8");
		resolver.setUploadTempDir(new ServletContextResource(this.getServletContext() ,"data/file_upload_tmp"));
		return resolver;
	}
	
	@Bean(name = "requestMappingHandlerAdapter")
	public RequestMappingHandlerAdapter requestMappingHandlerAdapter(){
		RequestMappingHandlerAdapter rmha = new RequestMappingHandlerAdapter();
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
		for(HttpMessageConverter<?> converter : super.requestMappingHandlerAdapter().getMessageConverters()){
			if(!converter.getClass().equals(jacksonMessageConverter.getClass())){
				messageConverters.add(converter);
			}
		}
		messageConverters.add(jacksonMessageConverter);
		rmha.setMessageConverters(messageConverters);
		return rmha;
	}
	
	
	@Bean
	public HandlerMapping resourceHandlerMapping() {
		return super.resourceHandlerMapping();
	}
	
	  
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/res/**").addResourceLocations("/res/");
        registry.addResourceHandler("/data/**").addResourceLocations("/data/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("/favicon.ico");
    }
	/*
	public MappingJackson2HttpMessageConverter jacksonMessageConverter(){
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        //Registering Hibernate5Module to support lazy objects
        mapper.setSerializationInclusion(Include.NON_EMPTY);
        Hibernate5Module hm = new Hibernate5Module();
        hm.enable(Hibernate5Module.Feature.FORCE_LAZY_LOADING);
        mapper.registerModule(hm);
        messageConverter.setObjectMapper(mapper);
        return messageConverter;
    }
	*/
	/*
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		//converters.add(fastJsonMessageConverter);
		super.configureMessageConverters(converters);
	}*/
	
}
